package mypractice;

//import java.lang.annotation.Target;
//import java.util.Arrays;
import java.util.Scanner;


public class Work3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n;//用于遍历
        StudentList[] instance = new StudentList[3];//创建实体数组
        //读取数据
        for (n = 0; n < 3; n++) {
            instance[n] = new StudentList();//初始化实例
            //一次搞定还是不行
//            String[] input = scanner.Line().split(" "); // 一次性输入姓名和年龄，以空格分隔
//            System.out.println(Arrays.toString(input));
//            instance[n].name = input[0];
//            instance[n].year = Integer.parseInt(input[1]);
            instance[n].name = scanner.next();
            instance[n].year = Integer.parseInt(scanner.next());
        }
        StudentList target = new StudentList();//增删改查的目标
//        String[] input = scanner.nextLine().split(" "); // 一次性输入姓名和年龄，以空格分隔
        target.name = scanner.next();
        target.year = Integer.parseInt(scanner.next());

        //用于主流程
        int temp;//0为成功
        lobel:
        while (true) {
            int i = Integer.parseInt(scanner.next());
            switch (i) {
                case 1:
                    studentList(instance);
                    break;
                case 2:
                    temp=addStudent(instance, target);
                    if(temp==0){
                        System.out.println("add student success");
                    }else{
                        System.out.println("add student fail");
                    }
                    break;
                case 3:
                    temp=delStudent(instance, target);
                    if(temp==0){
                        System.out.println("add student success");
                    }else{
                        System.out.println("add student fail");
                    }
                    break;
                case 4:
                    reviseStudent(instance, target);
                    break;
                case 5:
                    demandStudent(instance, target);
                    break;
                case 6:
                    boolean j = exitSystem(scanner);
                    if (j) {
                        break lobel;
                    } else {
                        break;
                    }
                default:
                    System.out.println("input false,please re-enter");
            }
        }
    }

    //打印学生列表
    public static void studentList(StudentList[] instance) {
        int i;
        for (i = 0; i < instance.length; i++) {
            instance[i].prin();
        }
    }

    //增加学生
    public static int addStudent(StudentList[] instance, StudentList target) {
        if (instance.length == 0) {
            System.out.println("instance.length==0");
            return -1;
        }
        int i;
        for (i = 0; i < instance.length; i++) {
            if (instance[i] == null || target.equals(instance[i])) {
                break;
            }
        }
        if (i == instance.length) {
            //Instance group is full
            System.out.println("Instance group is full");
            return -1;
        }
        if (instance[i] == null) {
            instance[i] = target;
            System.out.println("add target success");
            return 0;
        }
        if (target.equals(instance[i])) {
            System.out.println("Instance already exists");
            return -1;
        }
        return -1;
    }

    //删除学生
    public static int delStudent(StudentList[] instance, StudentList target) {
        if (instance.length == 0) {
            return -1;
        }
        int i;
        for (i = 0; i < instance.length; i++) {
            if (target.equals(instance[i])) {
                instance[i] = null;
                System.out.println("delStudent success");
                return 0;
            }
        }
        return 0;
    }

    //修改学生
    public static int reviseStudent(StudentList[] instance, StudentList target) {
        int i;
        if (instance.length == 0) {
            return -1;
        }
        for (i = 0; i < instance.length; i++) {
            if (target.equals(instance[i])) {
                target = instance[i];
                System.out.println("reviseStudent success");
                return 0;
            }
        }
        return 0;
    }

    //查询学生
    public static void demandStudent(StudentList[] instance, StudentList target) {
        int i;
        if (instance.length == 0) {
            return;
        }
        for (i = 0; i < instance.length; i++) {
            if (target.equals(instance[i])) {
                System.out.println("demandStudent success");
                return;
            }
        }
        return;
    }

    //退出系统
    public static boolean exitSystem(Scanner scanner) {
        System.out.println("Confirm to exit?  true please 1  false  please 2");
//        Scanner scanner = new Scanner(System.in);
        int i = Integer.parseInt(scanner.next());
        if (i == 1) {
            //结束循环
            return true;
        } else {
            //继续执行循环
            return false;
        }
    }
}

class StudentList {
    String name;
    int year;

    public void prin() {
        System.out.print(this.name + "  ");
        System.out.println(this.year + "  ");
    }
}